package com.kingstar.crawler.Test;

import com.kingstar.crawler.Utils.HttpUtils;
import com.kingstar.crawler.entity.GoodsInfo;
import com.kingstar.crawler.service.GoodsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王光华
 * @Description:
 * @date 2021/9/13 16:35
 */

@Component
public class GoodsTest {
    @Resource
    private HttpUtils httpUtils;

    @Autowired
    private GoodsService goodsService;

    public static final ObjectMapper MAPPER = new ObjectMapper();

    //private static final Logger logger = LoggerFactory.getLogger(GoodsTest.class);

    //设置定时任务执行完成后，再间隔100秒执行一次
    @Scheduled(fixedDelay = 1000 * 100)
    public void process() throws Exception {
        //分析页面发现访问的地址,页码page从1开始，下一页page加2
        String url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&suggest=1.his.0.0&wq=%E6%89%8B%E6%9C%BA&s=121&click=0&page=";


        //遍历执行，获取所有的数据
        for (int i = 1; i < 10; i = i + 2) {
            //发起请求进行访问，获取页面数据,先访问第一页
            String html = this.httpUtils.getHtml(url + i);

            //解析页面数据，保存数据到数据库中
            this.parseHtml(html);

        }
        System.out.println("保存成功");
    }


    //解析页面，并把数据保存到数据库中
    private void parseHtml(String html) throws Exception {
        //使用jsoup解析页面
        Document document = Jsoup.parse(html);

        //获取商品数据
        Elements spus = document.select("div#J_goodsList > ul > li");

        //遍历商品spu数据
        for (Element spuEle : spus) {
            //获取商品spu
            String attr = spuEle.attr("data-spu");
            //判断是否为空串
            long spu = Long.parseLong(attr.equals("")?"0":attr);

            //Long spu = Long.parseLong(spuEle.attr("data-spu"));

            //获取商品sku数据
            Elements skus = spuEle.select("li.ps-item img");
            for (Element skuEle : skus) {
                //获取商品sku
                Long sku = Long.parseLong(skuEle.attr("data-sku"));

                //判断商品是否被抓取过，可以根据sku判断
                GoodsInfo goodsInfo = new GoodsInfo();
                //goodsInfo.setSku(sku);
                //List<GoodsInfo> list = this.goodsService.findAll(goodsInfo);
                //判断是否查询到结果
//                if (list.size() > 0) {
//                    //如果有结果，表示商品已下载，进行下一次遍历
//                    continue;
//                }



                //商品spu
                goodsInfo.setSpu(spu);
                //商品sku
                goodsInfo.setSku(sku);
                //商品url地址
                goodsInfo.setUrl("https://item.jd.com/" + sku + ".html");



                //获取商品标题
                String itemHtml = this.httpUtils.getHtml(goodsInfo.getUrl());
                String title = Jsoup.parse(itemHtml).select("div.sku-name").text();
                goodsInfo.setTitle(title);

                //获取商品价格
                String priceUrl = "https://p.3.cn/prices/mgets?skuIds=J_"+sku;
                String priceJson = this.httpUtils.getHtml(priceUrl);
                //解析json数据获取商品价格
                double price = MAPPER.readTree(priceJson).get(0).get("p").asDouble();
                goodsInfo.setPrice(price);

                //保存商品数据
                this.goodsService.saveGoodsInfo(goodsInfo);
            }
        }
    }

}
